import { Component } from '@angular/core';
import { ApesField } from './apes-field';
import { ApiService } from '@core/services/http/api.service';
import { Events } from '@core/services/events/Events';
import { ExpressionService } from '@core/services/expression/ExpressionService';
import { SubjectService } from '@core/services/events/subject.service';
import { dateFormat } from '../../../../utils/utils';

@Component({
  selector: 'formly-field-date-year',
  template: `
    <apes-year-picker [apesStyle]="{'width': '100%'}"
                      style="width: 100%;"
                      [apesFormat]="formatType"
                      [disabled]="to.disabled"
                      [formControl]="formControl"
                      [formlyAttributes]="field"
                      [apesPlaceHolder]="to.placeholder"
                      (ngModelChange)="onChange($event)"></apes-year-picker>
  `,
  host: {},
})
export class FormlyFieldDateYear extends ApesField {
  // yyyy-MM-dd  yyyy-MM-dd HH:mm:ss  yyyy-MM-dd HH:mm:ss SSS
  value = null;
  formatType: string = 'yyyy';
  showTime = false;

  constructor(protected apiService: ApiService, protected events: Events, protected expressionService: ExpressionService, protected subject: SubjectService) {
    super(apiService, events, expressionService, subject);
  }


  onChange(date: Date): void {
    if (!date) {
      return;
    }
    this.value = dateFormat(this.formatType, new Date(date));
  }

  ngOnInit() {
    this.addDefalutPlaceholder('请选择');
    if (this.to['valueFormatter']) {
      this.formatType  = this.to['valueFormatter'];
      this.showTime = true;
    }
    if(this.formControl.value) {
      this.value = dateFormat(this.formatType, new Date(this.formControl.value));
    }
  }

}
